from . import route_food
from flask import render_template,g,request,jsonify
from models.food.FoodCategory import FoodCategory
from Service import Pagination,UserService
from app import db,app

# 分类

@route_food.route("/category")
def category():
    current_user = g.current_user
    page_num = int(request.args.get("page", 1))

    # 搜索
    status = int(request.args.get("status", -1))

    # 保存搜索状态
    condition = {
        "status": status
    }

    query = FoodCategory.query
    if status > -1:
        query = query.filter_by(status=status)

    # 分页
    totals = query.count()
    pagination = Pagination(page_num, totals)

    if totals != 0:
        data_list = query.offset(pagination["offset"]).limit(pagination["page_size"]).all()
    else:
        data_list = []
    pagination["data_list"] = data_list

    return render_template("food/category.html",current_user=current_user,pagination=pagination,condition=condition)


@route_food.route("/category-edit",methods=["GET","POST"])
def categoryEdit():
    current_user = g.current_user
    if request.method == "GET":
        id = request.args.get("id")
        food_category = FoodCategory.query.get(id)
        if food_category is None:
            return render_template("error/error.html")
        return render_template("food/category_edit.html",current_user=current_user,food_category=food_category)


    req_dict = request.values
    food_category = FoodCategory.query.get(req_dict["id"])

    if food_category is None:
        return render_template("error/error.html")

    food_category.name = req_dict["name"]
    food_category.weight = req_dict["weight"]

    food_category.updated_time = UserService.geneTime()

    try:
        db.session.add(food_category)
        db.session.commit()
    except:
        return jsonify({"code": -1, "msg": "修改失败！"})

    return jsonify({"code": 200, "msg": "修改成功！"})


@route_food.route("/category-insert",methods=["GET","POST"])
def categoryInsert():
    current_user = g.current_user
    if request.method == "GET":
        return render_template("food/category_insert.html", current_user=current_user)


    req_dict = request.values
    food_category = FoodCategory()

    food_category.name = req_dict["name"]
    food_category.weight = req_dict["weight"]

    food_category.created_time = food_category.updated_time = UserService.geneTime()

    try:
        db.session.add(food_category)
        db.session.commit()
    except:
        return jsonify({"code": -1, "msg": "添加失败！"})

    return jsonify({"code": 200, "msg": "添加成功！"})


@route_food.route("/category-remove",methods=["POST"])
def categoryRemove():
    try:
        id = request.form.get("id")
        food_category = FoodCategory.query.filter_by(id=id)
        food_category.update({"status": 0})
        # 将这个类别下的所有菜也删除
        cate_list = food_category.first().food   #依靠relationship
        for f in cate_list:
            f.status = 0

        db.session.commit()
    except:
        return jsonify({"code":-1,"msg":"删除失败！"})

    return jsonify({"code":200,"msg":"删除成功！"})


@route_food.route("/category-recover",methods=["POST"])
def categoryRecover():
    try:
        id = request.form.get("id")
        food_category = FoodCategory.query.filter_by(id=id)
        food_category.update({"status": 1})
        # 将这个类别下的所有菜也恢复
        cate_list = food_category.first().food  # 依靠relationship
        for f in cate_list:
            f.status = 1

        db.session.commit()
    except:
        return jsonify({"code":-1,"msg":"恢复失败！"})

    return jsonify({"code":200,"msg":"恢复成功！"})